package com.activeMQ.student.topic;

import lombok.SneakyThrows;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import java.io.IOException;

/**
 * 主题的消费者
 * @date 2020/10/7 15:26
 */
public class TestConsumer {
    private static String brokerURL = "tcp://106.12.51.117:61617";
    private static String topicName = "topic-Hello";

    public static void main(String[] args) throws Exception{

        ConnectionFactory factory = new ActiveMQConnectionFactory(brokerURL);

        Connection connection = factory.createConnection();

        connection.start();

        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        Topic topic = session.createTopic(topicName);

        MessageConsumer consumer = session.createConsumer(topic);

        System.out.println("1号消费者");
        consumer.setMessageListener(new MessageListener() {
            @SneakyThrows
            public void onMessage(Message message) {
                if (message instanceof TextMessage){
                    TextMessage textMessage = (TextMessage) message;

                    System.out.println(textMessage.getText());
                }
            }
        });

        System.in.read();
        session.close();
        connection.close();
        System.out.println("主题消息接收完成");
    }

}
